package com.pattern.objectcreator.prototype;

import java.util.ArrayList;

/**
 * @author yangguanzan
 * @date 3/18/22
 * @description
 */
public class RealPrototype implements BasePrototype {

    private String name;

    private ArrayList list;

    public RealPrototype(String name) {
        this.name = name;
    }
    /**
     * 浅克隆
     *
     * @return 对象
     */
    @Override
    public RealPrototype clone() {
        try {
            return (RealPrototype)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 深克隆,简单实现的深克隆
     *
     * @return 对象
     */
    @Override
    public RealPrototype deepClone() {

        try {
            RealPrototype prototype = (RealPrototype)super.clone();
            prototype.setList((ArrayList)this.list.clone());
            return prototype;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getName() {
        return this.name;
    }
    @Override
    public ArrayList getList() {
        return this.list;
    }

    @Override
    public void setList(ArrayList list) {
         this.list = list;
    }
    @Override
    public void setName(String name) {
        this.name = name;
    }
}
